﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;

namespace InkCanvasControl
{
    public class BasePropertyCtrl : ContentControl
    {
        static BasePropertyCtrl()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(BasePropertyCtrl), new FrameworkPropertyMetadata(typeof(BasePropertyCtrl)));
        }

        #region 变量及常量
        private const string PART_Popup = "PART_Popup";
        #endregion

        #region 内部成员
        internal Popup P_Popup { get; private set; }
        #endregion

        #region 注册属性

        #region 注册Sys_PropertyCtrlType(根据控件类型选择不同的样式)
        public PropertyCtrlTypes Sys_PropertyCtrlType
        {
            get { return (PropertyCtrlTypes)GetValue(Sys_PropertyCtrlTypeProperty); }
            set { SetValue(Sys_PropertyCtrlTypeProperty, value); }
        }
        public static readonly DependencyProperty Sys_PropertyCtrlTypeProperty =
            DependencyProperty.Register("Sys_PropertyCtrlType",
            typeof(PropertyCtrlTypes),
            typeof(BasePropertyCtrl),
            new PropertyMetadata(PropertyCtrlTypes.WithCommand));
        #endregion

        #region 注册Sys_ValueDisplayLayoutType(选择值的显示位置)
        public ValueDisplayLayoutTypes Sys_ValueDisplayLayoutType
        {
            get { return (ValueDisplayLayoutTypes)GetValue(Sys_ValueDisplayLayoutTypeProperty); }
            set { SetValue(Sys_ValueDisplayLayoutTypeProperty, value); }
        }
        public static readonly DependencyProperty Sys_ValueDisplayLayoutTypeProperty =
            DependencyProperty.Register("Sys_ValueDisplayLayoutType",
            typeof(ValueDisplayLayoutTypes),
            typeof(BasePropertyCtrl),
            new PropertyMetadata(ValueDisplayLayoutTypes.TopRight));
        #endregion

        #region Popup相关

        #region 注册Sys_PopupIsOpen(Popup是否打开)
        public bool Sys_PopupIsOpen
        {
            get { return (bool)GetValue(Sys_PopupIsOpenProperty); }
            set { SetValue(Sys_PopupIsOpenProperty, value); }
        }
        public static readonly DependencyProperty Sys_PopupIsOpenProperty =
            DependencyProperty.Register("Sys_PopupIsOpen",
            typeof(bool),
            typeof(BasePropertyCtrl),
            new PropertyMetadata(false, Sys_PopupIsOpenPropertyChanged));
        private static void Sys_PopupIsOpenPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            BasePropertyCtrl ctrl = d as BasePropertyCtrl;
            if (ctrl != null&&ctrl.P_Popup!=null)
            {
                ctrl.P_Popup.IsOpen =(bool)e.NewValue;
            }
        }
        #endregion 

        #region 注册Sys_PopupPlacement(Popup位置模式)
        public PlacementMode Sys_PopupPlacement
        {
            get { return (PlacementMode)GetValue(Sys_PopupPlacementProperty); }
            set { SetValue(Sys_PopupPlacementProperty, value); }
        }
        public static readonly DependencyProperty Sys_PopupPlacementProperty =
            DependencyProperty.Register("Sys_PopupPlacement",
            typeof(PlacementMode),
            typeof(BasePropertyCtrl),
            new PropertyMetadata(PlacementMode.Left));
        #endregion 

        #region 注册Sys_PopupHorizontalOffset(Popup位置的水平偏移)
        public double Sys_PopupHorizontalOffset
        {
            get { return (double)GetValue(Sys_PopupHorizontalOffsetProperty); }
            set { SetValue(Sys_PopupHorizontalOffsetProperty, value); }
        }
        public static readonly DependencyProperty Sys_PopupHorizontalOffsetProperty =
            DependencyProperty.Register("Sys_PopupHorizontalOffset",
            typeof(double),
            typeof(BasePropertyCtrl),
            new PropertyMetadata(0.0));
        #endregion 

        #region 注册Sys_PopupVerticalOffset(Popup位置的垂直偏移)
        public double Sys_PopupVerticalOffset
        {
            get { return (double)GetValue(Sys_PopupVerticalOffsetProperty); }
            set { SetValue(Sys_PopupVerticalOffsetProperty, value); }
        }
        public static readonly DependencyProperty Sys_PopupVerticalOffsetProperty =
            DependencyProperty.Register("Sys_PopupVerticalOffset",
            typeof(double),
            typeof(BasePropertyCtrl),
            new PropertyMetadata(0.0));
        #endregion 

        #endregion
  
        #region 注册Sys_PopupContent(需要弹出的内容)

        public object Sys_PopupContent
        {
            get { return (object)GetValue(Sys_PopupContentProperty); }
            set { SetValue(Sys_PopupContentProperty, value); }
        }
        public static readonly DependencyProperty Sys_PopupContentProperty =
            DependencyProperty.Register("Sys_PopupContent",
            typeof(object),
            typeof(BasePropertyCtrl),
            new PropertyMetadata(null, OnSys_SelectImagePropertyChanged));
        private static void OnSys_SelectImagePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        #endregion

        #endregion

        #region 重写
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            this.P_Popup = GetTemplateChild(PART_Popup) as Popup;
            //this.P_Popup.Opened+=new EventHandler(P_Popup_Opened);
            this.P_Popup.Closed += new EventHandler(P_Popup_Closed);
        }

        void P_Popup_Closed(object sender, EventArgs e)
        {
            this.Sys_PopupIsOpen = false;
        }

        void P_Popup_Opened(object sender, EventArgs e)
        {
            if (Sys_PopupPlacement == PlacementMode.Top)
            {
                Sys_PopupHorizontalOffset = -((this.P_Popup.Child as FrameworkElement).ActualWidth - this.ActualWidth) / 2;
            }
            if (Sys_PopupPlacement == PlacementMode.Bottom)
            {
                Sys_PopupHorizontalOffset = -((this.P_Popup.Child as FrameworkElement).ActualWidth - this.ActualWidth) / 2;
            }
            if (Sys_PopupPlacement == PlacementMode.Left)
            {
                Sys_PopupVerticalOffset = -((this.P_Popup.Child as FrameworkElement).ActualHeight - this.ActualHeight) / 2;
            }
            if (Sys_PopupPlacement == PlacementMode.Right)
            {
                Sys_PopupVerticalOffset = -((this.P_Popup.Child as FrameworkElement).ActualHeight - this.ActualHeight) / 2;
            }
        }
        #endregion
    }

    public enum ValueDisplayLayoutTypes
    {
        Center,
        TopRight
    }
    public enum PropertyCtrlTypes
    {
        Common,
        WithCommand
    }
}
